<?php
require_once('includes/config.php');
require_once('includes/functions/func.global.php');
require_once('includes/functions/func.users.php');
require_once('includes/classes/class.template_engine.php');
require_once('includes/lang/lang_'.$config['lang'].'.php');

// Start the session
session_start();

// Connect to the database
db_connect($config);

// Check if the user has a remember cookie set
checkremember($config);

// Check that the user is logged in
logincheck();

// Get site categories
$cats = get_cats($config,$lang);

if(isset($_POST['cmd']))
{
	$_GET['cmd'] = $_POST['cmd'];
}

if(!isset($_GET['cmd']))
{
	$_GET['cmd'] = 'details';
}

switch ($_GET['cmd']) 
{
	// Edit Details page
	case 'details':
		if(isset($_POST['email']))
		{
			mysql_query("UPDATE `".$config['db']['pre']."users` SET `email` = '".validate_input($_POST['email'])."',`commentst` = '".validate_input($_POST['commentst'])."' WHERE `user_id` =".validate_input($_SESSION['duser']['id'])." LIMIT 1 ;");
		}
		
		$user_details = mysql_fetch_array(mysql_query("SELECT email,commentst FROM ".$config['db']['pre']."users WHERE user_id='".validate_input($_SESSION['duser']['id'])."' LIMIT 1"));
		
		$page = new HtmlTemplate ("templates/" . $config['tpl_name'] . "/editprofile_details.html");
		$page->SetLoop ('CATS', $cats);
		$page->SetParameter ('EMAIL_FIELD', $user_details['email']);
		$page->SetParameter ('COMMENTST_FIELD', $user_details['commentst']);
		$page->SetParameter ('OVERALL_HEADER', create_header($config,$lang,$cats,$lang['EDITPROFILE']));
		$page->SetParameter ('OVERALL_FOOTER', create_footer($config,$lang));
		$page->CreatePageEcho($lang,$config);
		break;
	// Avatar manage page
	case 'avatar':
		if(isset($_GET['remove']))
		{
			removeuserimg($_SESSION['duser']['id']);
		
			mysql_query("UPDATE `".$config['db']['pre']."users` SET `avatar` = '0' WHERE `user_id` ='".validate_input($_SESSION['duser']['id'])."' LIMIT 1 ;");
		}
	
		if(isset($_FILES['avatarfile']))
		{
			$fpath = getuimgpath($_SESSION['duser']['id'],true,$config);
			$newpath = $fpath.'/tempimg';
		
			if($_FILES['avatarfile']['error'])
			{
			}
			else
			{
				if (move_uploaded_file($_FILES['avatarfile']['tmp_name'], $newpath)) 
				{
					resizeimg($newpath,16,16,$fpath.'s'.$_SESSION['duser']['id'].'.jpg');
					resizeimg($newpath,48,48,$fpath.'m'.$_SESSION['duser']['id'].'.jpg');
					resizeimg($newpath,64,64,$fpath.'l'.$_SESSION['duser']['id'].'.jpg');
					
					mysql_query("UPDATE `".$config['db']['pre']."users` SET `avatar` = '1' WHERE `user_id` ='".validate_input($_SESSION['duser']['id'])."' LIMIT 1 ;");
				}
			}
			
			if(file_exists($newpath))
			{
				unlink($newpath);
			}
		}
	
		$cavatar = 0;
		$avatar_path = '';
		
		$uinfo = mysql_fetch_array(mysql_query("SELECT avatar FROM ".$config['db']['pre']."users WHERE user_id='".validate_input($_SESSION['duser']['id'])."' LIMIT 1"));
		
		if($uinfo['avatar'])
		{
			$cavatar = 1;
			$avatar_path = getuimgpath($_SESSION['duser']['id'],false,$config).'l'.$_SESSION['duser']['id'].'.jpg';
		}
	
		$page = new HtmlTemplate ("templates/" . $config['tpl_name'] . "/editprofile_avatar.html");
		$page->SetLoop ('CATS', $cats);
		$page->SetParameter ('AVATAR', $cavatar);
		$page->SetParameter ('AVATAR_PATH_LARGE', $avatar_path);
		$page->SetParameter ('OVERALL_HEADER', create_header($config,$lang,$cats,$lang['MANAVATAR']));
		$page->SetParameter ('OVERALL_FOOTER', create_footer($config,$lang));
		$page->CreatePageEcho($lang,$config);
		break;
}
?>